package com.morizhang.merchanmanager.api.plugins.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.morizhang.merchanmanager.api.ApiResult;
import com.morizhang.merchanmanager.api.IService;
import com.morizhang.merchanmanager.api.bean.ApiDefinitionHolder;
import com.morizhang.merchanmanager.api.bean.ErrorDefinitionHolder;
import com.morizhang.merchanmanager.api.bean.MerChanManagerException;
import com.morizhang.merchanmanager.api.dao.JobListDao;
import com.morizhang.merchanmanager.api.model.JobModel;
import com.morizhang.merchanmanager.api.plugins.BasicJobManage;
import com.morizhang.merchanmanager.api.plugins.PluginAnnotation;
import com.morizhang.merchanmanager.api.utils.SerialNoGenerator;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@PluginAnnotation(name = "BasicJobManage")
public class BasicJobManageImpl implements BasicJobManage {
    @Autowired
    private JobListDao jobListDao;

    @Override
    public void assignJob(JobModel jobModel) throws Exception {
        jobModel.setSerialNo(SerialNoGenerator.newSerialNo());
        jobListDao.addJobList(jobModel);
    }

    @Override
    public List<JobModel> getJobList(String userID) {
        return jobListDao.queryJobListByUserID(userID);
    }

    @Override
    public void checkCompleteAuthority(String userID, String serialNo) throws MerChanManagerException {
        JobModel searchResult = jobListDao.queryJobListBySerialNo(serialNo);
        if (searchResult == null) {
            MerChanManagerException exception = new MerChanManagerException();
            exception.setErrorDefinition(ErrorDefinitionHolder.get("COMPLETE_JOB_NO_SUCH_JOB"));
            throw exception;
        }
        if (searchResult.getUserID().compareTo(userID) != 0) {
            MerChanManagerException exception = new MerChanManagerException();
            exception.setErrorDefinition(ErrorDefinitionHolder.get("COMPLETE_JOB_CANNOT_COMPLETE_BY_THIS_USER"));
            throw exception;
        }
    }

    @Override
    public ApiResult completeJob(String serialNo) throws MerChanManagerException {
        JobModel searchResult = jobListDao.queryJobListBySerialNo(serialNo);
        if (searchResult == null) {
            MerChanManagerException exception = new MerChanManagerException();
            exception.setErrorDefinition(ErrorDefinitionHolder.get("COMPLETE_JOB_NO_SUCH_JOB"));
            throw exception;
        }
        String jobDetail = searchResult.getJobDetail();
        //执行jobDetail
        JSONObject detailJson = JSONObject.parseObject(jobDetail, Feature.OrderedField);
        String requestService = detailJson.getString("requestService");
        IService service = ApiDefinitionHolder.get(requestService);
        try {
            ApiResult executeResult = service.callService(detailJson);
            jobListDao.deleteJobList(serialNo);
            return executeResult;
        } catch (Exception e) {
            MerChanManagerException exception = new MerChanManagerException();
            exception.setErrorDefinition(ErrorDefinitionHolder.get("COMPLETE_JOB_ERROR_OCCUR_WHILE_EXECUTE"));
            throw exception;
        }
    }
}
